package com.jsbs.iam.user.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.jsbs.iam.common.core.dto.Result;
import com.jsbs.iam.common.core.utils.Json;
import com.jsbs.iam.common.core.utils.StringUtils;
import com.jsbs.iam.user.constant.Constants;
import com.jsbs.iam.user.dto.ApplyConfigAddDto;
import com.jsbs.iam.user.dto.ApplyConfigDeleteDto;
import com.jsbs.iam.user.dto.ApplyConfigGetDto;
import com.jsbs.iam.user.dto.ApplyConfigSelectDto;
import com.jsbs.iam.user.service.ApplyConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @BelongsProject: iam
 * @BelongsPackage: com.jsbs.iam.user.controller
 * @Author: WangJiYuan
 * @Description: 应用中心   应用配置
 * @Version: 2023-01-04  11:46
 */
@RestController
@RequestMapping("/iam/user/applyConfig")
public class ApplyConfigController extends BaseController{

    private static final Logger logger = LoggerFactory.getLogger(ApplyConfigController.class);

    @Autowired
    private ApplyConfigService applyConfigService;

    private static final String ADMIN_USER = "admin";
    /**
     * 新增应用数据
     * @param applyConfigAddDto
     * @return
     */
    @PostMapping("/insetApplyData")
    public Result insetApplyData(HttpServletRequest httpServletRequest, @RequestBody ApplyConfigAddDto applyConfigAddDto) {
        logger.info("Route==>ApplyConfigController method=insetApplyData 新增应用数据，入参：{}", JSON.toJSONString(applyConfigAddDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>ApplyConfigController method=>insetApplyData 新增应用数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>ApplyConfigController method=>insetApplyData 新增应用数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                logger.warn("Route=>ApplyConfigController method=>insetApplyData 新增应用数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            applyConfigAddDto.setCompanyCode(companyCode);
            applyConfigAddDto.setCreatePerson(userName);

            if (StringUtils.isNull(applyConfigAddDto.getApplyName())) {
                logger.info("新增应用数据失败{}", JSON.toJSONString(applyConfigAddDto));
                return Result.failWithMsg("应用名称为空");
            } else if (applyConfigAddDto.getApplyName().length() > 30) {
                logger.info("新增应用数据失败{}", JSON.toJSONString(applyConfigAddDto));
                return Result.failWithMsg("应用名称字数不能超过30");
            }
            if (StringUtils.isNull(applyConfigAddDto.getRequestUrl())) {
                logger.info("新增应用数据失败{}", JSON.toJSONString(applyConfigAddDto));
                return Result.failWithMsg("应用链接为空");
            }
            if (StringUtils.isNotNull(applyConfigAddDto.getApplyRemark())) {
                if (applyConfigAddDto.getApplyRemark().length() > 200) {
                    logger.info("新增应用数据失败{}", JSON.toJSONString(applyConfigAddDto));
                    return Result.failWithMsg("应用备注数据字数不能超过200字");
                }
            }
            //新增应用数据
            applyConfigService.insetApplyData(applyConfigAddDto);
            return Result.successWithMsg("新增应用数据成功");
        } catch (Exception e) {
            logger.error("Route==>ApplyConfigController method=>insetApplyData 新增应用数据异常，入参：{}，异常：{}", JSON.toJSONString(applyConfigAddDto), e);
            return handleException(e);
        }
    }

    /**
     * 获取应用数据
     * @param applyConfigSelectDto
     * @return
     */
    @PostMapping("/getApplyData")
    public Result getApplyData(HttpServletRequest httpServletRequest,@RequestBody ApplyConfigSelectDto applyConfigSelectDto) {
        logger.info("Route==>ApplyConfigController method=getApplyData 获取应用数据，入参：{}", JSON.toJSONString(applyConfigSelectDto));
        try {
            // todo  wjy  临时获取companyCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String adminRoleCode = httpServletRequest.getHeader(Constants.ADMIN_ROLE_CODE);
            companyCode = "000001";
            userCode = "admin";
            adminRoleCode = "JS0030";
            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>ApplyConfigController method=>getApplyData 获取应用数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            applyConfigSelectDto.setCompanyCode(companyCode);
            applyConfigSelectDto.setCreatePerson(userCode);
            applyConfigSelectDto.setAdminRoleCode(adminRoleCode);

            //获取应用数据
            PageInfo pageInfo = applyConfigService.getApplyData(applyConfigSelectDto);
            return new Result(Result.SUCCESS, "获取应用数据成功", pageInfo);
        } catch (Exception e) {
            logger.error("Route==>ApplyConfigController method=>getApplyData 获取应用数据异常，入参：{}，异常：{}", JSON.toJSONString(applyConfigSelectDto), e);
            return handleException(e);
        }
    }

    /**
     * 修改应用数据
     * @param applyConfigAddDto
     * @return
     */
    @PostMapping("/updateApplyData")
    public Result updateApplyData(HttpServletRequest httpServletRequest, @RequestBody ApplyConfigAddDto applyConfigAddDto) {
        logger.info("Route==>ApplyConfigController method=updateApplyData 修改应用数据，入参：{}", JSON.toJSONString(applyConfigAddDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>ApplyConfigController method=>updateApplyData 修改应用数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>ApplyConfigController method=>updateApplyData 修改应用数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                logger.warn("Route=>ApplyConfigController method=>updateApplyData 修改应用数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            applyConfigAddDto.setCompanyCode(companyCode);
            applyConfigAddDto.setCreatePerson(userName);

            if (StringUtils.isNull(applyConfigAddDto.getApplyCode())) {
                logger.info("修改应用数据失败{}", JSON.toJSONString(applyConfigAddDto));
                return Result.failWithMsg("应用编码为空");
            }
            if (StringUtils.isNull(applyConfigAddDto.getApplyName())) {
                logger.info("修改应用数据失败{}", JSON.toJSONString(applyConfigAddDto));
                return Result.failWithMsg("应用名称为空");
            } else if (applyConfigAddDto.getApplyName().length() > 30) {
                logger.info("修改应用数据失败{}", JSON.toJSONString(applyConfigAddDto));
                return Result.failWithMsg("应用名称字数不能超过30");
            }
            if (StringUtils.isNull(applyConfigAddDto.getRequestUrl())) {
                logger.info("修改应用数据失败{}", JSON.toJSONString(applyConfigAddDto));
                return Result.failWithMsg("应用链接为空");
            }
            if (StringUtils.isNotNull(applyConfigAddDto.getApplyRemark())) {
                if (applyConfigAddDto.getApplyRemark().length() > 200) {
                    logger.info("修改应用数据失败{}", JSON.toJSONString(applyConfigAddDto));
                    return Result.failWithMsg("应用备注数据字数不能超过200字");
                }
            }
            //修改应用数据
            applyConfigService.updateApplyData(applyConfigAddDto);
            return Result.successWithMsg("修改应用数据成功");
        } catch (Exception e) {
            logger.error("Route==>ApplyConfigController method=>updateApplyData 修改应用数据异常，入参：{}，异常：{}", JSON.toJSONString(applyConfigAddDto), e);
            return handleException(e);
        }
    }

    /**
     * 批量删除应用数据
     * @param applyConfigDeleteDto
     * @return
     */
    @PostMapping("/deleteApplyData")
    public Result deleteApplyData(HttpServletRequest httpServletRequest, @RequestBody ApplyConfigDeleteDto applyConfigDeleteDto) {
        logger.info("Route=>ApplyConfigController method=>deleteApplyData 批量删除应用数据：{}", Json.toJsonString(applyConfigDeleteDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>ApplyConfigController method=>updateApplyData 修改应用数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>ApplyConfigController method=>updateApplyData 修改应用数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                logger.warn("Route=>ApplyConfigController method=>updateApplyData 修改应用数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            applyConfigDeleteDto.setCompanyCode(companyCode);
            applyConfigDeleteDto.setUpdatePerson(userName);

            if (org.springframework.util.CollectionUtils.isEmpty(applyConfigDeleteDto.getApplyCodes())) {
                return new Result(Result.FAIL, "请选择数据再进行删除！");
            }
            //批量删除应用数据
            applyConfigService.deleteApplyData(applyConfigDeleteDto);
            return new Result(Result.SUCCESS, "删除成功");
        } catch (Exception e) {
            logger.error("Route==>ApplyConfigController method=>deleteApplyData 批量删除应用数据异常，入参：{}，异常：{}", Json.toJsonString(applyConfigDeleteDto), e);
            return handleException(e);
        }
    }

    /**
     * 获取应用的账号列表数据
     * @param httpServletRequest
     * @param applyConfigGetDto
     * @return
     */
    @PostMapping("/getAccountData")
    public Result getAccountData(HttpServletRequest httpServletRequest,@RequestBody ApplyConfigGetDto applyConfigGetDto) {
        logger.info("Route==>ApplyConfigController method=getApplyData 获取应用的账号列表数据，入参：{}", JSON.toJSONString(applyConfigGetDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String adminRoleCode = httpServletRequest.getHeader(Constants.ADMIN_ROLE_CODE);
            companyCode = "000001";
            userCode = "admin";
            adminRoleCode = "JS0030";

            applyConfigGetDto.setCompanyCode(companyCode);
            applyConfigGetDto.setCreatePerson(userCode);
            applyConfigGetDto.setRoleCode(adminRoleCode);

            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>ApplyConfigController method=>getAccountData 获取应用的账号列表数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>ApplyConfigController method=>getAccountData 获取应用的账号列表数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            if (!ADMIN_USER.equals(userCode) && StringUtils.isEmpty(adminRoleCode)) {
                logger.warn("Route=>ApplyConfigController method=>getAccountData 获取应用的账号列表数据，当前登录人角色信息异常");
                return Result.failWithMsg("登录人角色信息异常");
            }
            if (StringUtils.isEmpty(applyConfigGetDto.getApplyCode())) {
                logger.warn("Route=>ApplyConfigController method=>getAccountData 获取应用的账号列表数据，异常");
                return Result.failWithMsg("应用编码不可为空!!!");
            }
            //获取应用的账号列表数据
            PageInfo pageInfo = applyConfigService.getAccountData(applyConfigGetDto);
            return Result.success("获取应用的账号列表数据成功", pageInfo);
        } catch (Exception e) {
            logger.error("Route==>ApplyConfigController method=>getApplyData 获取应用的账号列表数据，入参：{}，异常：{}", JSON.toJSONString(applyConfigGetDto), e);
            return handleException(e);
        }
    }

}
